/**
 * Cells register moving queries, but don't register moving objects.
 * Created on 02-11-2010
 * 
 * A Rectangle specifies an area in a coordinate 
 * space that is enclosed by the Rectangle object's 
 * top-left point (x, y) in the coordinate space, 
 * its width, and its height.
 * 
 * The coordinate relies on the labeling (id) of the cell.
 * The top left coordinate of cell (x, y) can be calculated by 
 * (x*a, y*a). The cells grow downward.  
 * @author Xiaohui
 */
package entity;

import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class Cell extends Rectangle {
	public int row_num; // row number
	public int col_num; // col number

	public int sideLength;
	public List<Integer> registeredQueries; // the list of queries registered
											// with
	// the cell
	public List<Integer> registeredObjects;
	public static int initCapacity = 16;

	public void addQuery(int moid) {
		if (registeredQueries == null) {
			registeredQueries = new ArrayList<Integer>(initCapacity);
		}
		this.registeredQueries.add(moid);
	}

	// every cell is a square
	/**
	 * the lower left point has coordinates: cid_x, cid_y
	 */
	public Cell(int cid_x, int cid_y, int length) {
		super(cid_x * length, cid_y * length, length, length);
		this.row_num = cid_x;
		this.col_num = cid_y;
		this.sideLength = length;
		this.registeredQueries = null;
		registeredObjects = new ArrayList<Integer>(initCapacity);
		registeredQueries = new ArrayList<Integer>(initCapacity);
	}

	public boolean equals(Cell anotherCell) {
		return (this.row_num == anotherCell.row_num)
				&& (this.col_num == anotherCell.col_num);
	}

	/**
	 * This method ensures the uniquness of a cell object in a cell set.
	 */
	public boolean equals(Object e) {
		if (!(e instanceof Cell)) {
			return false;
		}
		if (this.row_num == ((Cell) e).row_num
				&& this.col_num == ((Cell) e).col_num) {
			return true;
		} else {
			return false;
		}
	}

	public boolean isIntersectWith(Ellipse2D circle) {
		return circle.intersects(this);
	}

	public int[] getCellId() {
		int[] ret = { this.row_num, this.col_num };
		return ret;
	}

	public void removeQuery(int qid) {
		this.registeredQueries.remove(qid);
	}

	@Override
	public String toString() {
		String s = "cell:[" + row_num + "," + col_num + "]";
		// if (this.registeredQueries != null) {
		// s = s + this.registeredQueries.toString();
		// }

		return s;
	}

	public static void main(String[] args) {
		List<Integer> l = new ArrayList<Integer>();
		l.add(10);
		l.add(20);
		System.out.println(l);
		l.remove(new Integer(20));
		System.out.println(l);
	}
}
